<?php
  $basename = dirname($argv[0]) . DIRECTORY_SEPARATOR;

  $lockfile = $basename . "lapbs.lock";
  
  if(file_exists($lockfile))
  {
    die("Lockfile '" . $lockfile . "' exists.\n");
  }

  $fp = fopen($lockfile, "w");
  fwrite($fp, date("Y-m-d H:i:s"));
  fclose($fp);
  
  require_once($basename . 'classes/InterfaceJob.php');
  require_once($basename . 'classes/JobStatus.php');
  require_once($basename . 'classes/Job.php');
  require_once($basename . 'classes/JobMysql.php');
  require_once($basename . 'classes/JobSvn.php');
  require_once($basename . 'classes/JobXenserver.php');
  require_once($basename . 'classes/InterfaceNotification.php');
  require_once($basename . 'classes/Notification.php');
  require_once($basename . 'classes/NotificationSendEmail.php');
  require_once($basename . 'classes/Notifier.php');
  require_once($basename . 'classes/Backup.php');
  require_once($basename . 'log4php/Logger.php');

  if(!is_dir($basename . 'logs'))
  {
    mkdir($basename . 'logs');    
  }
  
  $dailyLogfile = $basename . 'logs' . DIRECTORY_SEPARATOR . date("Ymd") . "_lapbs.log";
  
  $logger = Logger::getRootLogger();
  $appenderFile = new LoggerAppenderFile();
  $appenderFile->setAppend(true);
  $appenderFile->setFileName($dailyLogfile);
  $appenderFile->setLayout(new LoggerLayoutTTCC());
  $appenderFile->setThreshold(LoggerLevel::ALL);
  $appenderFile->activateOptions();
  $logger->addAppender($appenderFile);
  
  $b = new Backup();
  $b->loadConfig($basename . 'config.xml');
  if($b->validConfig())
  {
    $b->loadJobs();
    $b->doJobs();
    $b->sendStatus();
  }
  
  unlink($lockfile);